package code;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");resp.setContentType("text/plain;charset=utf8");
        String[]names ={"刘梦媛","李娜","覃韦越","陈贺鑫"};String[]passes={"202121091036","202121091016","202121091010","202121091034"};
        String name=req.getParameter("name");String pass=req.getParameter("pass");
        HttpSession session=req.getSession();
        boolean a=false;
        //登录验证
        for(int i=0;i<names.length;i++){
            if(names[i].equals(name)&&passes[i].equals(pass)){
                a=true;
                System.out.println("登录成功！");
                //加入身份，方便后期读取谁说的
                session.setAttribute("name",names[i]);
                break;
            }
        }
        //加入cookie
        if(a==true) {
            if ("on".equals(req.getParameter("rem_pass"))) {
                System.out.println("设置cookie");
                Cookie cookie=new Cookie("right_login","1");
                cookie.setMaxAge(60*5);
                resp.addCookie(cookie);
                Cookie[] cookies=req.getCookies();
                if(cookies!=null) {
                    System.out.println("--------------登录后cookie属性，并加入cookie------------");
                    for (Cookie asd : cookies) {
                        System.out.println(asd.getName() + asd.getValue());
                    }
                }
            }
            resp.sendRedirect("chat.jsp");
        }
        else{
            System.out.println("登录失败");
            resp.sendRedirect("fail.html");
        }
    }
}
